const program = require("commander");
const chalk = require("chalk");
const inquirer = require("inquirer");
const moment = require("moment");
const path = require('path');
const {
  createProjectAction,
  addComponentAction,
  addPageAction,
  ctrlAction,
  pushAction,
} = require("./actions");

const createCommands = () => {
  // 创建我家哥哥的
  program
    .command("ctrl")
    .description("进入我家哥哥的网页")
    .action(async () => {
      const firstAnswer = await inquirer.prompt([
        {
          type: "list",
          name: "choiseType",
          message: "你是真爱粉还是小黑子？",
          choices: ["真爱粉", "纯鹿人", "小黑子"],
        },
      ]);
      const ikunMap = {
        真爱粉:
          "https://www.bilibili.com/video/BV1DJ411n7jv?spm_id_from=333.337.search-card.all.click&vd_source=57f15a57a90c560fde95bd5ea179a33f",
        纯鹿人: "坤流才是最屌的！！！！",
        小黑子:
          "https://weibo.com/caizicaixukun?from=page_230673_profile&wvr=6&mod=like&is_all=1",
      };
      if (ikunMap[firstAnswer.choiseType].includes("https")) {
        ctrlAction(ikunMap[firstAnswer.choiseType]);
        return;
      }
      console.log(chalk.cyan(ikunMap[firstAnswer.choiseType]));
    });

  // 创建项目
  program
    .command("create <project> [others...]")
    .description("clone a repository into a folder")
    .action(createProjectAction);

  // 创建vue组件
  program
    .command("addcpn <name>")
    .description(
      "add vue component, 例如: ikun addcpn HelloWorld [-d 路径支持path.reslove ]"
    )
    .action((name) => {
      addComponentAction(name, program.dest || "./");
    });

  // 创建一个vuePage
  program
    .command("addpage <name>")
    .description(
	"add vue page, 例如: ikun addpage test [-d 路径支持path.reslove ]")
    .action((name) => {
		addPageAction(name, program.dest || "./pages");
    });

  // 提交git
  program
    .command("push")
    .description("git push -u origin master")
    .action(async () => {
      // 1.cover一下没有commit的时候
      const nowTimeWord =
        moment(new Date()).format("YYYY-MM-DD hh:mm:ss") + "提交";
      const commitWord = process.argv[3] || nowTimeWord;
      const pathName = path.resolve(program.dest || './')
      // console.log(pathName)
      // 2.判断下有无参数 
      if (process.argv[3]) {
        pushAction(commitWord, pathName);
        return
      }
      // 3. 用默认的还是用自己输入的
      const choicesCommitType = await inquirer.prompt([
        {
          type: "list",
          name: "type",
          message: "选择commit方式",
          choices: ["默认", "手动填写", '摆烂！'],
          default: 0
        },
      ]); 
      if (choicesCommitType["type"] === "默认") {
        pushAction(commitWord, pathName); 
        return 
      }
      if (choicesCommitType["type"] === "摆烂！") {
        return 
      }
      //手动填写情况下
      const handleWord = await inquirer.prompt([
        {
          type: "input",
          name: "commit",
          message: "填写你的commit！",
        },
      ]); 
      pushAction(handleWord.commit, pathName)
    });
};

module.exports = createCommands;
